package com.supinfo.food.dao.jpa;

import java.util.List;

import javax.persistence.EntityManager;

import com.supinfo.food.dao.MenuDao;
import com.supinfo.food.entities.Menu;

/**
 * Menu DAO
 * @author Syn
 */
public class JpaMenuDao implements MenuDao{

	/** Entity manager */
	private EntityManager entityManager;
	
	public JpaMenuDao() {
		entityManager = ManagerDAO.getEntityManager();
	}
	
	@Override
	public Menu addMenu(Menu menu) {
		entityManager.getTransaction().begin();
		entityManager.persist(menu);
		entityManager.getTransaction().commit();
		return menu;
	}

	@Override
	public Menu findMenu(Long id) {
		return entityManager.find(Menu.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Menu> getAllMenus() {
		return entityManager.createQuery("SELECT m FROM Menu m").getResultList();
	}

	@Override
	public void removeMenu(Menu menu) {
		entityManager.getTransaction().begin();
		entityManager.remove(menu);
		entityManager.getTransaction().commit();
	}

	@Override
	public Menu updateMenu(Menu menu) {
		entityManager.merge(menu);
		return menu;
	}

}
